package com.colin.datamove.service.impl;

import com.colin.datamove.entity.DepartmentModel;
import com.colin.datamove.mapper.db1.AssetDepartmentDao;
import com.colin.datamove.service.MoveService;
import com.colin.datamove.util.LogUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("assetDepartmentService")
public class AssetDepartmentServiceImpl implements MoveService {

    @Resource
    private AssetDepartmentDao db1AssetDepartmentDao;
    @Resource
    private com.colin.datamove.mapper.db2.AssetDepartmentDao db2AssetDepartmentDao;

    @Override
    public void moveJob(int pmc) {
        // 查询老数据库需要迁移数据
        List<DepartmentModel> unMoveData = db2AssetDepartmentDao.getUnMoveData(pmc);
        LogUtil.info("AssetDepartmentService 此次待迁移数据:{}条", unMoveData.size());
        if (CollectionUtils.isNotEmpty(unMoveData)) {
            // 更新数据为已迁移
            int i = db2AssetDepartmentDao.updateMoved(unMoveData);
            LogUtil.info("AssetDepartmentService 更新待迁移数据为已迁移:{}条", i);

            // 插入新数据库
            int i1 = db1AssetDepartmentDao.insertData(unMoveData);
            LogUtil.info("AssetDepartmentService 新库中插入数据:{}条", i1);
        }

        // 更新新数据库中的主键关联数据，
        int i2 = db1AssetDepartmentDao.updateParentId();
        LogUtil.info("AssetDepartmentService 新库中更新parentId数据:{}条", i2);

        int i3 = db1AssetDepartmentDao.updateCompanyId();
        LogUtil.info("AssetDepartmentService 新库中更新companyId数据:{}条", i3);

        int i4 = db1AssetDepartmentDao.updateRootCompanyId();
        LogUtil.info("AssetDepartmentService 新库中更新rootCompanyId数据:{}条", i4);
    }


}
